<template>
  <el-dialog
    v-model="addFormVisible"
    title="添加用户"
    :append-to-body="true"
    @close="closeDialog"
    destroy-on-close
  >
    <el-tabs class="tabs" v-model="activeName">
      <el-tab-pane label="用户信息" name="userInfo">
        <el-form
          :model="adduserForm"
          :rules="adduserFormrules"
          ref="adduserFormRef"
          label-width="80"
          label-position="right"
        >
          <el-form-item label="用户名称" prop="userName">
            <el-input v-model="adduserForm.userName"></el-input>
          </el-form-item>

          <el-form-item label="姓">
            <el-input v-model="adduserForm.surname"></el-input>
          </el-form-item>
          <el-form-item label="名">
            <el-input v-model="adduserForm.name"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="adduserForm.password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input type="password" v-model="adduserForm.confirmPassword"></el-input>
          </el-form-item>
          <el-form-item label="邮箱地址" prop="email">
            <el-input v-model="adduserForm.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="adduserForm.phoneNumber"></el-input>
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="adduserForm.lockoutEnabled"
              >登录失败,账户被锁定</el-checkbox
            >
            <el-checkbox v-model="adduserForm.isActive"
              >是否激活</el-checkbox
            >
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="角色" name="roleInfo">
        <el-checkbox-group v-model="adduserForm.roleNames">
          <el-checkbox
            v-for="item in roleList"
            :value="item.name"
            :key="item.id"
            :label="item.name"
          >
          </el-checkbox>
        </el-checkbox-group>
      </el-tab-pane>
      <el-tab-pane label="组织架构" name="orgInfo">
        <el-tree
          v-model="adduserForm.orgIds"
          node-key="id"
          default-expand-all
          :props="treeProps"
          :data="orgTree"
          show-checkbox
          ref="orgTreeRef"
        />
      </el-tab-pane>
    </el-tabs>
    <div class="modal-footer">
      <el-button type="primary" @click="saveuser">保存</el-button>
    </div>
  </el-dialog>
</template>
<script setup lang="ts">
import { getAllRoles } from "@/api/identity/role/index";
import { isEmpty } from "@/utils/index";
import { addUser, addUserToOrganization } from "@/api/identity/user/index";
import { getOrganizationunitForTree } from "@/api/identity/organizationUnits/index";
import { FormInstance,FormRules } from 'element-plus'


const activeName = ref<string>("");
activeName.value = "userInfo";

const addFormVisible = ref<boolean>(true);
const adduserForm = reactive<any>({

  isActive:true
});
const roleList = ref<any>([]);
const adduserFormRef = ref<FormInstance>();
const orgTree = ref<any>([]);
const treeProps = {
  children: "child",
  label: "displayName",
};
const confirmPasswordRule = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入确认密码"));
  } else if (value !== adduserForm.password) {
    callback(new Error("两次输入密码不一致!"));
  } else {
    callback();
  }
};
const adduserFormrules = ref<FormRules>({
  userName: [{ required: true, message: "请输入用户名称", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  confirmPassword: [{ validator: confirmPasswordRule, trigger: "blur" }],
  email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
});
const getRoles = async () => {
  roleList.value = (await getAllRoles()).items;
  //console.log("roleList", roleList.value);
};
const orgTreeRef = ref<any>();

const emit = defineEmits(["closedialogevent", "refresh"]);

const getOrgTree = async () => {
  orgTree.value = await getOrganizationunitForTree({});
  //console.log("orgTree", orgTree.value);
};

const closeDialog = () => {
  addFormVisible.value = false;
  emit("closedialogevent", addFormVisible.value);
};

const saveuser = () => {
  let checkNodes = orgTreeRef.value.getCheckedNodes();
  adduserFormRef.value?.validate((valid, fields) => {
    if (valid) {
      let param = adduserForm;
      param.orgIds = checkNodes.map((item: any) => {
        return item.id;
      });
      //console.log("createuser", param);
      addUserToOrganization(param).then((res) => {
        if (res) {
          ElNotification({
            title: "提示",
            message: "添加用户成功",
            type: "success",
            duration: 2000,
          });
          closeDialog();
          emit("refresh");
        } else {
          closeDialog();
          return false;
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

onMounted(async () => {
  await getRoles();
  await getOrgTree();
});
</script>
<style scoped></style>
